<?php
namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

class MenuController extends BCommonController
{

    protected $redirectTo = '/backstage/menu/index';
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $maxPid = CommonModel::getField('menu',['pid'=>0],'id')->id;
        // dd($maxPid);
        $list = BackstageModel::menu();
        // dd($list);
        return view('backstage/menu/index',['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function add()
    {
        $menu = BackstageModel::menu();
        // dd($list);
        return view('backstage/menu/add',compact('menu'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function doadd(Request $request)
    {
        // dd($request->all());exit;
        //驗證
        $info = $request->except('_token');// 請求數據中除了_token以外的值
        if(empty($info['menu_name']))  return response()->json(['status'=>'0','message'=>'參數錯誤！']);
        $lv = CommonModel::getField('menu','level',['id'=>intval($_POST['pid'])]);
        $data = array(
            'pid' => intval($_POST['pid']),
            'level'=>$lv+1,
            'menu_name' => trim($_POST['menu_name']),
            'url' => trim($_POST['url']),
            'icon' => trim($_POST['icon']),
            'action' => trim($_POST['action']),
            'add_time' => time()
        );
        if(isset($_POST['attr_value'])){
            $key = $_POST['attr_value'];
            $val = $_POST['attr_name'];
            $arr = array_combine($key,$val);
            $data['has_permissions'] = serialize($arr);
        }
        $res = CommonModel::addData('menu',$data);
        if($res){
            return response()->json(['status'=>'1','message'=>'操作成功！','jumpUrl'=>'/backstage/menu/index']);
        }else{
            return response()->json(['status'=>'0','message'=>'操作失敗！']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // return view('backstage/menu/add');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $field = ['id','pid','menu_name','url','icon','action','has_permissions'];
        $info = CommonModel::getOne('menu',['id'=>$id],$field);
        $menu = BackstageModel::menu();
        // dd(unserialize($info->has_permissions));
        if(!empty($info)){
            if(!empty($info->has_permissions)){
                $info->has_permissions = json_encode(array_reverse(unserialize($info->has_permissions)));
            }else{
                $info->has_permissions = '';
            }
            
        }
        // dd($info->has_permissions);
        return view('backstage/menu/edit',compact('info','menu'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function doedit(Request $request)
    {
        //驗證
        $info = $request->except('_token');// 請求數據中除了_token以外的值
        $id = intval($info['id']);
        if(empty($info['menu_name']))  return response()->json(['status'=>'0','message'=>'參數錯誤！']);
        $lv = CommonModel::getField('menu','level',['id'=>intval($_POST['pid'])]);
        $data = array(
            'pid' => intval($_POST['pid']),
            'level'=>$lv+1,
            'menu_name' => trim($_POST['menu_name']),
            'url' => trim($_POST['url']),
            'icon' => trim($_POST['icon']),
            'action' => trim($_POST['action']),
        );
        if(isset($_POST['attr_value'])){
            $key = $_POST['attr_value'];
            $val = $_POST['attr_name'];
            $arr = array_combine($key,$val);
            $data['has_permissions'] = serialize($arr);
        }
        $res = CommonModel::updateData('menu',['id'=>$id],$data);
        if($res){
            return response()->json(['status'=>'1','message'=>'操作成功！','jumpUrl'=>'/backstage/menu/index']);
        }else{
            return response()->json(['status'=>'0','message'=>'操作失敗！']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    // 菜單
    public function getMenu($id)
    {
        $list = BackstageModel::getMenu(['pid'=>$id]);
        return $list;
    }
    public function fa_img(){
        return view('backstage/menu/faImg');
    }
}
